package Home_Work.src.day05;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * 要求用户输入若干员工信息，格式为：
 * name,age,gender,salary,hiredate;name,age,gender,salary,hiredate;....
 * 例如:
 * 张三,25,男,5000,2006-02-15;李四,26,女,6000,2007-12-24;...
 * 然后将每个员工信息解析成Emp对象。并存入到一个List集合中。
 * 并对集合排序，然后输出每个员工信息。
 * 
 * 再根据员工的入职时间排序，入职晚的在前，早的在后并
 * 输出每个员工的信息。
 * @author Bonnie
 *
 */
public class Test03 {
	public static void main(String[] args) throws ParseException {
		System.out.println("请输入员工信息(name,age,gender,salary,hiredate)：");
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine();
		String[] st = str.split("\\;");
		List<Emp> list = new ArrayList<Emp>();
		Emp person;
		String name;
		int age;
		String gender;
		int salary;
		Date hiredate;//入职时间
		String[] arr;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(int i = 0; i < st.length; i++) {
			arr = st[i].split("\\,");
			name = arr[0];
			age = Integer.parseInt(arr[1]);
			gender = arr[2];
			salary = Integer.parseInt(arr[3]);
			hiredate = sdf.parse(arr[4]);
			person = new Emp(name,age,gender,salary,hiredate);
			list.add(person);
		}
		list.sort(null);
		for(Emp emp : list){
			System.out.println(emp);
		}
		System.out.println("按照入职时间从晚到早排序:");
		Comparator<Emp> com = new Comparator<Emp>(){
			public int compare(Emp e1, Emp e2) {
				long time = e2.getHiredate().getTime()-e1.getHiredate().getTime();
				return time>0?1:-1;
			}			
		};
		list.sort(com);
		for(Emp e : list){
			System.out.println(e);
		}
	}
}
